package com.jg.poiet.excel.po;

import com.jg.poiet.util.CellUtils;
import com.jg.poiet.util.StyleUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

public class ICell {

    public final static String HORIZONTAL_ALIGNMENT_LEFT = "left";
    public final static String HORIZONTAL_ALIGNMENT_CENTER = "center";
    public final static String HORIZONTAL_ALIGNMENT_RIGHT = "right";

    public final static String VERTICAL_ALIGNMENT_START = "start";
    public final static String VERTICAL_ALIGNMENT_CENTER = "center";
    public final static String VERTICAL_ALIGNMENT_END = "end";

    private String value = "";
    private double width;
    private double height;
    private String backgroundColor;
    private int colSpan = 1;
    private int rowSpan = 1;
    /**
     * 水平对齐方式
     */
    private String horizontalAlignment = HORIZONTAL_ALIGNMENT_LEFT;

    /**
     * 垂直对齐方式
     */
    private String verticalAlignment = VERTICAL_ALIGNMENT_START;
    private String leftBorderColor;
    private String rightBorderColor;
    private String topBorderColor;
    private String bottomBorderColor;
    private String fontColor;
    private Double fontSize;
    private boolean bold;

    public ICell(XSSFWorkbook workbook, XSSFSheet sheet, XSSFRow row, XSSFCell cell, int columnIndex) {
        if (cell == null) {
            this.width = sheet.getColumnWidthInPixels(columnIndex);
            this.height = row.getHeightInPoints() * (72.0 / 96.0) * 2;
            return ;
        }
        XSSFCellStyle cellStyle = cell.getCellStyle();
        this.value = CellUtils.getCellValue(cell);
        this.width = sheet.getColumnWidthInPixels(cell.getColumnIndex());
        this.height = row.getHeightInPoints() * (72.0 / 96.0) * 2;
        //判断是否合并单元格
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
            if (region.isInRange(row.getRowNum(), cell.getColumnIndex())) {
                //是合并单元格,判断是否起始
                if (region.getFirstRow() == row.getRowNum() && region.getFirstColumn() == cell.getColumnIndex()) {
                    this.rowSpan = region.getLastRow() - region.getFirstRow() + 1;
                    this.colSpan = region.getLastColumn() - region.getFirstColumn() + 1;
                } else {
                    this.rowSpan = 0;
                    this.colSpan = 0;
                }
                break;
            }
        }
        //背景色
        if (cellStyle.getFillForegroundXSSFColor() != null) {
            this.backgroundColor = cellStyle.getFillForegroundXSSFColor().getARGBHex().substring(2);
        }
        //文字水平对齐方式
        if (cellStyle.getAlignment() != null) {
            switch (cellStyle.getAlignment()) {
                case LEFT: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_LEFT;break;
                case CENTER: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_CENTER;break;
                case RIGHT: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_RIGHT;break;
                default: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_LEFT;
            }
        }
        //文字垂直对齐方式
        if (cellStyle.getVerticalAlignment() != null) {
            switch (cellStyle.getVerticalAlignment()) {
                case TOP: this.verticalAlignment = VERTICAL_ALIGNMENT_START;break;
                case CENTER: this.verticalAlignment = VERTICAL_ALIGNMENT_CENTER;break;
                case BOTTOM: this.verticalAlignment = VERTICAL_ALIGNMENT_END;break;
                default: this.verticalAlignment = VERTICAL_ALIGNMENT_START;
            }
        }
        //设置边框
        if (cellStyle.getBorderLeft() != null && cellStyle.getBorderLeft() != BorderStyle.NONE) {
            XSSFColor color = cellStyle.getLeftBorderXSSFColor();
            if (color != null && color.getARGBHex() != null) {
                this.leftBorderColor = color.getARGBHex().substring(2);
            } else {
                this.leftBorderColor = "000000";
            }
        }
        if (cellStyle.getBorderRight() != null && cellStyle.getBorderRight() != BorderStyle.NONE) {
            XSSFColor color = cellStyle.getRightBorderXSSFColor();
            if (color != null && color.getARGBHex() != null) {
                this.rightBorderColor = color.getARGBHex().substring(2);
            } else {
                this.rightBorderColor = "000000";
            }
        }
        if (cellStyle.getBorderTop() != null && cellStyle.getBorderTop() != BorderStyle.NONE) {
            XSSFColor color = cellStyle.getTopBorderXSSFColor();
            if (color != null && color.getARGBHex() != null) {
                this.topBorderColor = color.getARGBHex().substring(2);
            } else {
                this.topBorderColor = "000000";
            }
        }
        if (cellStyle.getBorderBottom() != null && cellStyle.getBorderBottom() != BorderStyle.NONE) {
            XSSFColor color = cellStyle.getBottomBorderXSSFColor();
            if (color != null && color.getARGBHex() != null) {
                this.bottomBorderColor = color.getARGBHex().substring(2);
            } else {
                this.bottomBorderColor = "000000";
            }
        }
        //设置字体
        XSSFFont font = cellStyle.getFont();
        //字体颜色
        if (font.getXSSFColor() != null && font.getXSSFColor().getARGBHex() != null) {
            this.fontColor = font.getXSSFColor().getARGBHex().substring(2);
        } else {
            this.fontColor = "000000";
        }
        //字体大小
        if (font.getFontHeightInPoints() > 0) {
            this.fontSize = StyleUtils.pointsToPixels(font.getFontHeightInPoints());
        } else {
            this.fontSize = 14D;
        }
        //是否加粗
        this.bold = font.getBold();
    }

    public ICell(HSSFWorkbook workbook, HSSFSheet sheet, HSSFRow row, HSSFCell cell, int columnIndex) {
        if (cell == null) {
            this.width = sheet.getColumnWidthInPixels(columnIndex);
            this.height = StyleUtils.pointsToPixels(row.getHeightInPoints()) * 2;
            return ;
        }
        HSSFCellStyle cellStyle = cell.getCellStyle();
        this.value = CellUtils.getCellValue(cell);
        this.width = sheet.getColumnWidthInPixels(cell.getColumnIndex());
        this.height = StyleUtils.pointsToPixels(row.getHeightInPoints()) * 2;
        //判断是否合并单元格
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress region = sheet.getMergedRegion(i);
            if (region.isInRange(row.getRowNum(), cell.getColumnIndex())) {
                //是合并单元格,判断是否起始
                if (region.getFirstRow() == row.getRowNum() && region.getFirstColumn() == cell.getColumnIndex()) {
                    this.rowSpan = region.getLastRow() - region.getFirstRow() + 1;
                    this.colSpan = region.getLastColumn() - region.getFirstColumn() + 1;
                } else {
                    this.rowSpan = 0;
                    this.colSpan = 0;
                }
                break;
            }
        }
        //背景色
        if (cellStyle.getFillForegroundColorColor() != null && cellStyle.getFillPattern() != FillPatternType.NO_FILL) {
            this.backgroundColor = StyleUtils.getHexColorFromRgb(cellStyle.getFillForegroundColorColor().getTriplet());
        }
        //文字水平对齐方式
        if (cellStyle.getAlignment() != null) {
            switch (cellStyle.getAlignment()) {
                case LEFT: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_LEFT;break;
                case CENTER: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_CENTER;break;
                case RIGHT: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_RIGHT;break;
                default: this.horizontalAlignment = HORIZONTAL_ALIGNMENT_LEFT;
            }
        }
        //文字垂直对齐方式
        if (cellStyle.getVerticalAlignment() != null) {
            switch (cellStyle.getVerticalAlignment()) {
                case TOP: this.verticalAlignment = VERTICAL_ALIGNMENT_START;break;
                case CENTER: this.verticalAlignment = VERTICAL_ALIGNMENT_CENTER;break;
                case BOTTOM: this.verticalAlignment = VERTICAL_ALIGNMENT_END;break;
                default: this.verticalAlignment = VERTICAL_ALIGNMENT_START;
            }
        }
        //设置边框
        if (cellStyle.getBorderLeft() != null && cellStyle.getBorderLeft() != BorderStyle.NONE) {
            this.leftBorderColor = StyleUtils.getHexColor(cellStyle.getLeftBorderColor());
        }
        if (cellStyle.getBorderRight() != null && cellStyle.getBorderRight() != BorderStyle.NONE) {
            this.rightBorderColor = StyleUtils.getHexColor(cellStyle.getRightBorderColor());
        }
        if (cellStyle.getBorderTop() != null && cellStyle.getBorderTop() != BorderStyle.NONE) {
            this.topBorderColor = StyleUtils.getHexColor(cellStyle.getTopBorderColor());
        }
        if (cellStyle.getBorderBottom() != null && cellStyle.getBorderBottom() != BorderStyle.NONE) {
            this.bottomBorderColor = StyleUtils.getHexColor(cellStyle.getBottomBorderColor());
        }
        //设置字体
        HSSFFont font = cellStyle.getFont(workbook);
        //字体颜色
        this.fontColor = StyleUtils.getHexColor(font.getColor());
        //字体大小
        if (font.getFontHeightInPoints() > 0) {
            this.fontSize = StyleUtils.pointsToPixels(font.getFontHeightInPoints());
        } else {
            this.fontSize = 14D;
        }
        //是否加粗
        this.bold = font.getBold();
    }

    public boolean existsCell() {
        return this.rowSpan > 0 && this.colSpan > 0;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(String backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    public int getColSpan() {
        return colSpan;
    }

    public void setColSpan(int colSpan) {
        this.colSpan = colSpan;
    }

    public int getRowSpan() {
        return rowSpan;
    }

    public void setRowSpan(int rowSpan) {
        this.rowSpan = rowSpan;
    }

    public String getHorizontalAlignment() {
        return horizontalAlignment;
    }

    public void setHorizontalAlignment(String horizontalAlignment) {
        this.horizontalAlignment = horizontalAlignment;
    }

    public String getVerticalAlignment() {
        return verticalAlignment;
    }

    public void setVerticalAlignment(String verticalAlignment) {
        this.verticalAlignment = verticalAlignment;
    }

    public String getLeftBorderColor() {
        return leftBorderColor;
    }

    public void setLeftBorderColor(String leftBorderColor) {
        this.leftBorderColor = leftBorderColor;
    }

    public String getRightBorderColor() {
        return rightBorderColor;
    }

    public void setRightBorderColor(String rightBorderColor) {
        this.rightBorderColor = rightBorderColor;
    }

    public String getTopBorderColor() {
        return topBorderColor;
    }

    public void setTopBorderColor(String topBorderColor) {
        this.topBorderColor = topBorderColor;
    }

    public String getBottomBorderColor() {
        return bottomBorderColor;
    }

    public void setBottomBorderColor(String bottomBorderColor) {
        this.bottomBorderColor = bottomBorderColor;
    }

    public String getFontColor() {
        return fontColor;
    }

    public void setFontColor(String fontColor) {
        this.fontColor = fontColor;
    }

    public Double getFontSize() {
        return fontSize;
    }

    public void setFontSize(Double fontSize) {
        this.fontSize = fontSize;
    }

    public boolean isBold() {
        return bold;
    }

    public void setBold(boolean bold) {
        this.bold = bold;
    }
}
